cmake_minimum_required(VERSION 3.16.3)
project(friend-server)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

set(target "friend-server")
set(target_client "friend-client")

# proto框架代码生成
set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
set(proto_files ${proto_path}/friend.proto ${proto_path}/user.proto ${proto_path}/message_storage.proto ${proto_path}/base.proto)
set(proto_sources "")
foreach(proto_file ${proto_files})
    get_filename_component(proto_name ${proto_file} NAME_WE)
    set(proto_h  ${CMAKE_CURRENT_BINARY_DIR}/${proto_name}.pb.h)
    set(proto_cc ${CMAKE_CURRENT_BINARY_DIR}/${proto_name}.pb.cc)
    if (NOT EXISTS ${proto_cc})
        add_custom_command(
            OUTPUT ${proto_h} ${proto_cc}
            COMMAND protoc
            ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I${proto_path} --experimental_allow_proto3_optional ${proto_file}
            DEPENDS ${proto_file}
            COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
        )
    endif()
    list(APPEND proto_sources ${proto_cc})
endforeach()

# odb框架代码生成
set(odb_path ${CMAKE_CURRENT_SOURCE_DIR}/../entity)
set(odb_files ${odb_path}/chat_session.hxx ${odb_path}/chat_session_member.hxx ${odb_path}/user_relation.hxx ${odb_path}/friend_apply.hxx)
set(odb_sources "")
foreach(odb_file ${odb_files})
    get_filename_component(odb_name ${odb_file} NAME_WE)
    set(odb_hxx  ${CMAKE_CURRENT_BINARY_DIR}/${odb_name}-odb.hxx)
    set(odb_cxx ${CMAKE_CURRENT_BINARY_DIR}/${odb_name}-odb.cxx)
    if(NOT EXISTS ${odb_cxx})
        add_custom_command(
            OUTPUT ${odb_cxx} 
            COMMAND odb  
            ARGS -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time ${odb_file}  
            DEPENDS ${odb_file} 
            COMMENT "生成ODB框架代码文件:" ${odb_cxx} 
        )
    endif()   
    # 将所有生成的框架源码的文件名保存起来 student-odb.cxx classes-odb.cxx
    list(APPEND odb_sources ${odb_cxx})
endforeach()

# 生成目标
set(src_files "") 
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src src_files)
add_executable(${target} ${src_files} ${proto_sources} ${odb_sources})
target_link_libraries(
    ${target} 
    -lgflags
    -lspdlog
    -lfmt
    -lbrpc
    -lssl
    -lcrypto
    -lprotobuf
    -lleveldb
    -letcd-cpp-api 
    -lcpprest
    -lcurl
    -ljsoncpp
    -ldl
    -pthread
    -lodb-mysql -lodb -lodb-boost -lboost_date_time
    -lcpr -lelasticlient
)
target_include_directories(${target} PUBLIC
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/../entity
    ${CMAKE_CURRENT_SOURCE_DIR}/../shared
    ${CMAKE_CURRENT_SOURCE_DIR}/../shared/infra
    ${CMAKE_CURRENT_SOURCE_DIR}/../shared/dal
)

set(test_files "")
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/test test_files)
add_executable(${target_client} ${test_files} ${proto_sources})
target_link_libraries(
    ${target_client} 
    -lgflags
    -lspdlog
    -lfmt
    -lbrpc
    -lssl
    -lcrypto
    -lprotobuf
    -lleveldb
    -letcd-cpp-api 
    -lcpprest
    -lcurl
    -ljsoncpp
    -ldl
    -pthread
    -lgtest
)

target_include_directories(${target_client} PUBLIC
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/../shared
    ${CMAKE_CURRENT_SOURCE_DIR}/../shared/infra
    ${CMAKE_CURRENT_SOURCE_DIR}/../shared/dal
)

INSTALL(TARGETS ${target} RUNTIME DESTINATION bin)
